package context.dao;

import context.model.Dynamic;
import context.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class FollowRepository {
    private final String SELECT_BY_USERID = "select * from follow where userid=?";
    private final String SELECT_BY_FOLLOW_USERID = "select * from follow where follow_userid=?";
    private final String INSERT_USER = "insert into follow(userid,follow_userid) values(?,?)";
    private JdbcTemplate jdbcTemplate;

    private Connection connection;
    @Autowired          //自动装配
    public FollowRepository(JdbcTemplate jdbcTemplate) throws SQLException {
        this.jdbcTemplate = jdbcTemplate;
        connection = jdbcTemplate.getDataSource().getConnection();
    }


    @Autowired
    public UserRepository mUserRepository;

    private User mapRow(ResultSet resultSet, int i) throws SQLException {
        User result = new User();
        result.setId(resultSet.getLong("userid"));//被关注人
        result.setId(resultSet.getLong("follow_id"));
        return result;
    }

    //增
    public int insertUser(final long userid, final long follow_userid) {

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(
                new PreparedStatementCreator() {
                    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                        PreparedStatement ps = con.prepareStatement(INSERT_USER, new String[]{"userid", "follow_userid"});
                        ps.setLong(1, userid);
                        ps.setLong(2, follow_userid);
                        return ps;
                    }
                }, keyHolder);
        System.out.println("自动插入id============================" + keyHolder.getKey().intValue());
        return keyHolder.getKey().intValue();

    }

    //查自己关注的人
    public List<User> findFollowByID(long id) {
        List<User> followList = new ArrayList<>();        //构建一个list,保存动态

        try {
//            Connection connection = jdbcTemplate.getDataSource().getConnection(); //获得数据库连接

            PreparedStatement preparedStatement = connection.prepareStatement(SELECT_BY_USERID);  //创建预备语句
            preparedStatement.setLong(1, id);        //设置sql语句中的参数值
            ResultSet resultSet = preparedStatement.executeQuery();       //查询
            while (resultSet.next()) {       //循环遍历结果集
                long follow_user_id = resultSet.getLong("follow_userid");
                System.out.println("follow_user_id" + follow_user_id);
                User user = mUserRepository.findUserByID(follow_user_id);
                followList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return followList;
    }

    //查关注自己的人
    public List<User> findFansByID(long id) {
        List<User> followList = new ArrayList<>();        //构建一个list,保存动态

        try {
//            Connection connection = jdbcTemplate.getDataSource().getConnection(); //获得数据库连接

            PreparedStatement preparedStatement = connection.prepareStatement(SELECT_BY_FOLLOW_USERID);  //创建预备语句
            preparedStatement.setLong(1, id);        //设置sql语句中的参数值
            ResultSet resultSet = preparedStatement.executeQuery();       //查询
            while (resultSet.next()) {       //循环遍历结果集
                long user_id = resultSet.getLong("userid");
                User user = mUserRepository.findUserByID(user_id);
                followList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return followList;
    }

    //删
    private static final String sql = "delete  from follow where user_id=? and follow_id = ?";

    public void delete(long user_id, long follow_id) {

        Object[] obj = new Object[]{user_id, follow_id};
        int i = jdbcTemplate.update(sql, obj);

        if (i > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }


    private String GET_ATTENTION_COUNT = "select count(*) form follow where userid=?";   //查询出“我”关注的用户数量

    private String GET_FANS_COUNT = "select count(*) form follow where follow_id=?";//查询粉丝数量


    public int FindAtttentionUserCount(long id) {
        int user_id_count = 0;
        try {
//            Connection connection = jdbcTemplate.getDataSource().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_ATTENTION_COUNT);
            preparedStatement.setLong(1, id);
            preparedStatement.executeQuery();   //执行查询
            ResultSet resultSet = preparedStatement.executeQuery();
            user_id_count = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user_id_count;
    }

    public int FindFansCount(long id) {
        int follow_id_count = 0;
        try {
//            Connection connection = jdbcTemplate.getDataSource().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_FANS_COUNT);
            preparedStatement.setLong(1, id);
            preparedStatement.executeQuery();   //执行查询
            ResultSet resultSet = preparedStatement.executeQuery();
            follow_id_count = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return follow_id_count;
    }
}